package com.wc.codeforces.DP.线性DP.Shift_Esc;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/12/21 10:56
 * @description
 * https://codeforces.com/contest/2049/problem/D
 */
public class Main {
    /**
     * 思路：
     * 线性DP
     * g[i][j] 表示 (1, 1) 位置 到达 (i, j) 位置 的最小值
     * f[i][j][k] 表示 表示从 (1, 1) 位置 到达 (i, j) 的位置, 该行移动了 k 次的最小值
     * 从上面来 i > 1, A = g[i - 1][j] + K * k + a[i][(j + k - 1) % m + 1]
     * 从左边来 j > 1, B = f[i][j - 1] + a[i][(j + k - 1) % m + 1]
     * f[i][j][k] = min{A, B}
     */
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 210;
    static long INF = 0x3f3f3f3fL * 0x3f3f3f3f;
    static int[][] a = new int[N][N];
    // f[i][j] 表示从 1, 1 到达 i, j 的位置, 该行移动了k次 的最小值
    static long[][][] f = new long[N][N][N];
    static long[][] g = new long[N][N];
    static int n, m;
    static long K;

    public static void main(String[] args) {
        int T = sc.nextInt();
        while (T-- > 0) {
            n = sc.nextInt();
            m = sc.nextInt();
            K = sc.nextLong();
            for (int i = 1; i <= n; i++) {
                for (int j = 1; j <= m; j++) a[i][j] = sc.nextInt();
            }
            // 初始化从上面来, 只能从 0, 1到达 1, 1
            for (int i = 0; i <= m; i++) g[0][i] = INF;
            for (int i = 0; i <= n; i++) g[i][0] = INF;
            g[0][1] = 0;
            for (int i = 1; i <= n; i++) {
                for (int j = 1; j <= m; j++) {
                    g[i][j] = INF;
                    for (int k = 0; k < m; k++) {
                        f[i][j][k] = INF;
                        // 从上面一行过来
                        f[i][j][k] = Math.min(f[i][j][k], g[i - 1][j] + K * k + a[i][(j + k - 1) % m + 1]);
                        // > 1 才能从左边过来
                        if (j > 1) f[i][j][k] = Math.min(f[i][j][k], f[i][j - 1][k] + a[i][(j + k - 1) % m + 1]);
                        g[i][j] = Math.min(f[i][j][k], g[i][j]);
                    }
                }
            }
            out.println(g[n][m]);
        }
        out.flush();
    }
}


class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}

